import { VXETable } from 'vxe-table';
import $ from "jquery"
/**
 * 模拟C#的padleft
 * @param {any} totalWidth 字符总长度
 * @param {any} paddingChar 补全的字符
 */
String.prototype.PadLeft = function (totalWidth, paddingChar) {
    if (paddingChar !== null) {
        return this.PadHelper(totalWidth, paddingChar, false);
    } else {
        return this.PadHelper(totalWidth, ' ', false);
    }
}

String.prototype.PadRight = function (totalWidth, paddingChar) {
    if (paddingChar !== null) {
        return this.PadHelper(totalWidth, paddingChar, true);
    } else {
        return this.PadHelper(totalWidth, ' ', true);
    }
}

String.prototype.PadHelper = function (totalWidth, paddingChar, isRightPadded) {
    if (this.length < totalWidth) {
        let paddingString = new String();
        for (let i = 1; i <= (totalWidth - this.length); i++) {
            paddingString += paddingChar;
        }
        if (isRightPadded) {
            return (this + paddingString);
        } else {
            return (paddingString + this);
        }
    } else {
        return this;
    }
}

/**日期格式转化 */
Date.prototype.Format = function (fmt) {
    let o = {
        "M+": this.getMonth() + 1, //月份
        "D+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "Q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(Y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (let k in o)
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    return fmt;
};

/**日期格式转化 */
Date.prototype.FormatUTC = function (fmt) {
    let o = {
        "M+": this.getUTCMonth() + 1, //月份
        "D+": this.getUTCDate(), //日
        "h+": this.getUTCHours(), //小时
        "m+": this.getUTCMinutes(), //分
        "s+": this.getUTCSeconds(), //秒
        "Q+": Math.floor((this.getUTCMonth() + 3) / 3), //季度
        "S": this.getUTCMilliseconds() //毫秒
    };
    if (/(Y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getUTCFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (let k in o)
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    return fmt;
};

(function ($) {
    $.fn.SizeChanged = function (handleFunction) {
        let element = this;
        let lastWidth = element.width();
        let lastHeight = element.height();
        setInterval(function () {
            if (lastWidth === element.width() && lastHeight === element.height())
                return;
            if (typeof (handleFunction) === 'function') {
                handleFunction({
                    width: lastWidth,
                    height: lastHeight
                }, {
                    width: element.width(),
                    height: element.height()
                });
                lastWidth = element.width();
                lastHeight = element.height();
            }
        }, 100);
        return element;
    };

    (function ($) {
        $.fn.dragDiv = function (options) {
            return this.each(function () {
                var _moveDiv = $(this); //需要拖动的Div
                var _moveArea = options ? $(options) : $(document); //限定拖动区域，默认为整个文档内
                var isDown = false; //mousedown标记
                //ie的事件监听，拖拽div时禁止选中内容，firefox与chrome已在css中设置过-moz-user-select: none; -webkit-user-select: none;
                if (document.attachEvent) {
                    _moveDiv[0].attachEvent('onselectstart', function () {
                        return false;
                    });
                }
                _moveDiv.mousedown(function (event) {
                    var e = event || window.event;
                    //拖动时鼠标样式
                    _moveDiv.css("cursor", "move");
                    //获得鼠标指针离DIV元素左边界的距离
                    var x = e.pageX - _moveDiv.offset().left;
                    //获得鼠标指针离DIV元素上边界的距离 
                    var y = e.pageY - _moveDiv.offset().top;
                    _moveArea.on('mousemove', function (event) {
                        var ev = event || window.event;
                        //获得X轴方向移动的值 
                        var abs_x = ev.pageX - x;
                        //获得Y轴方向移动的值 
                        var abs_y = ev.pageY - y;
                        //div动态位置赋值
                        _moveDiv.css({
                            'left': abs_x,
                            'top': abs_y
                        });
                    });
                });
                _moveDiv.mouseup(function () {
                    _moveDiv.css('cursor', 'default');
                    //解绑拖动事件
                    _moveArea.off('mousemove');

                });

            });
        };
    })(jQuery);
    
}(jQuery));

window.alertErr = function (msg = "", id = "alertErr", time = 5000) {
    VXETable.modal.message({
        status: 'error',
        content: msg,
        id: id,
        duration: time,
        size: "medium",
        top: 80
        /* offset: 80,
        duration: time ,
        showClose: true,
        center: true */
    });
}

window.alertInfo = function (msg = "", id = "alertInfo", time = 3000) {
    VXETable.modal.message({
        status: 'info',
        content: msg,
        id: id || "",
        top: 60,
        size: "medium",
        duration: time,
        // "show-close": true,
        // "showClose": true,
        // "destroy-on-close": true,
        // center: true
    });
};

window.alertSucc = (msg = "", id = "alertSucc", time = 3000) => {
    VXETable.modal.message({
        status: 'success',
        content: msg,
        id: id,
        top: 60,
        duration: time,
        size: "medium",
        // showClose: true,
        // center: true,
        // "show-close": true,
        // "showClose": true,
        // "destroy-on-close": true
    });
}

window.alertWarn = (msg = "", id = "alertWarn", time = 5000) => {
    VXETable.modal.message({
        status: 'warning',
        content: msg,
        id: id,
        top: 60,
        duration: time,
        size: "medium",
        // showClose: true,
        // center: true,
        // "show-close": true,
        // "showClose": true,
        // "destroy-on-close": true
    });
}